using System;
using System.Collections.Generic;
using System.Text;
using System.Workflow.Activities;

namespace OrderWorkflow
{    
    [ExternalDataExchange]
    public interface IOrderStateMachineDataExchangeService
    {
        event EventHandler<OrderEventArgs> OrderCreated;
        event EventHandler<OrderEventArgs> OrderShipped;
        event EventHandler<OrderEventArgs> OrderUpdated;
        event EventHandler<OrderEventArgs> OrderProcessed;
        event EventHandler<OrderEventArgs> OrderCanceled;
    }

    [Serializable]
    public class OrderEventArgs : ExternalDataEventArgs
    {
        private Guid _orderId;

        public OrderEventArgs(Guid instanceId, Guid orderId)
            : base(instanceId)
        {
            _orderId = orderId;
        }

        public Guid OrderId
        {
            get { return _orderId; }
            set { _orderId = value; }
        }
    }

    [Serializable]
    public class OrderStateMachineDataExchangeService : IOrderStateMachineDataExchangeService
    {
        public event EventHandler<OrderEventArgs> OrderCreated;
        public event EventHandler<OrderEventArgs> OrderShipped;
        public event EventHandler<OrderEventArgs> OrderUpdated;
        public event EventHandler<OrderEventArgs> OrderProcessed;
        public event EventHandler<OrderEventArgs> OrderCanceled;

        public void RaiseOrderCreatedEvent(Guid orderId, Guid instanceId)
        {
            if (OrderCreated != null)
            {                
                OrderCreated(this, new OrderEventArgs(instanceId, orderId));
            }
        }

        public void RaiseOrderShippedEvent(Guid orderId, Guid instanceId)
        {
            if (OrderShipped != null)
            {
                OrderShipped(this, new OrderEventArgs(instanceId, orderId));
            }
        }

        public void RaiseOrderUpdatedEvent(Guid orderId, Guid instanceId)
        {
            if (OrderUpdated != null)
            {
                OrderUpdated(this, new OrderEventArgs(instanceId, orderId));
            }
        }

        public void RaiseOrderProcessedEvent(Guid orderId, Guid instanceId)
        {
            if (OrderProcessed != null)
            {
                OrderProcessed(this, new OrderEventArgs(instanceId, orderId));
            }
        }
        public void RaiseOrderCanceledEvent(Guid orderId, Guid instanceId)
        {
            if (OrderCanceled != null)
            {
                OrderCanceled(this, new OrderEventArgs(instanceId, orderId));
            }
        }       
    }
}
